/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Golf_Manager;

import Golf_Manager.model.GroupDescModel;
import Golf_Manager.sqlite.GroupDAO;
import Golf_Manager.vo.GroupDesc;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import static java.awt.image.ImageObserver.WIDTH;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.JTable;
/**
 *
 * @author Administrator
 */
public class GroupListDialog extends javax.swing.JDialog {

    /**
     * A return status code - returned if Cancel button has been pressed
     */
    public static final String RET_CANCEL = "";
    /**
     * A return status code - returned if OK button has been pressed
     */
   

    /**
     * Creates new form NewOkCancelDialog
     */
    public GroupListDialog(java.awt.Frame parent, String title, boolean modal) {
        super(parent, title, modal);
        initComponents();

        // Close the dialog when Esc is pressed
        String cancelName = "cancel";
        InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
        ActionMap actionMap = getRootPane().getActionMap();
        actionMap.put(cancelName, new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                    returnGdate = "";
                    setVisible(false);
                    dispose();
            }
        });
        
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(JTextField.CENTER);
        jTableGroupListTable.getColumnModel().getColumn(0).setPreferredWidth(60);
        jTableGroupListTable.getColumnModel().getColumn(0).setCellRenderer(renderer);
        jTableGroupListTable.getColumnModel().getColumn(1).setPreferredWidth(150);
        jTableGroupListTable.getColumnModel().getColumn(1).setCellRenderer(renderer);
        jTableGroupListTable.getColumnModel().getColumn(2).setPreferredWidth(150);
        jTableGroupListTable.getColumnModel().getColumn(2).setCellRenderer(renderer);
        jTableGroupListTable.getColumnModel().getColumn(3).setPreferredWidth(80);
        jTableGroupListTable.getColumnModel().getColumn(3).setCellRenderer(renderer);
        
        jTableGroupListTable.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    JTable target = (JTable)e.getSource();
                    int row = target.getSelectedRow();
                    int column = target.getSelectedColumn();
                    // do some action
                    //System.out.println("row :" + row + ", column :" + column);
                    Golf_Manager.model.GroupDescModel groupModel =  (Golf_Manager.model.GroupDescModel)jTableGroupListTable.getModel();
                    returnGdate = groupModel.getGroupDesc(row).getgDate();
                    setVisible(false);
                    dispose();
                }
            }
        });

        
//        Golf_Manager.model.GroupDescModel groupModel =  (Golf_Manager.model.GroupDescModel)jTableGroupListTable.getModel();
//        List<Golf_Manager.vo.GroupDesc> groupDescs = new ArrayList<>();
//        groupDescs.add(new GroupDesc("2011-01-01", "오름차순(낮은 것부터 높은 것순)", "오름차순(낮은 것부터 높은 것순)", "미반영"));
//        groupModel.setGroupDescs(groupDescs);
        Calendar oCalendar = Calendar.getInstance( );  
//        System.out.println(oCalendar.get(Calendar.YEAR)+"-01-01");
//        System.out.println(oCalendar.get(Calendar.YEAR)+"-12-31");
        
        try {
            SimpleDateFormat transFormat = new SimpleDateFormat("yyyy-MM-dd");     
            jDateFromGroupDate.setDate(transFormat.parse(oCalendar.get(Calendar.YEAR)+"-01-01"));
            jDateToGroupDate.setDate(transFormat.parse(oCalendar.get(Calendar.YEAR)+"-12-31"));
        } catch (ParseException ex) {
            Logger.getLogger(GroupListDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
                
        searchGroupList(oCalendar.get(Calendar.YEAR)+"-01-01", oCalendar.get(Calendar.YEAR)+"-12-31");
    }

    public String getReturnGdate() {
        return returnGdate;
    }

    /**
     * @return the return status of this dialog - one of RET_OK or RET_CANCEL
     */
   

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        okButton = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jDateFromGroupDate = new com.toedter.calendar.JDateChooser();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableGroupListTable = new javax.swing.JTable();
        jDateToGroupDate = new com.toedter.calendar.JDateChooser();
        jLabel1 = new javax.swing.JLabel();

        setResizable(false);

        okButton.setText("조회");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        jLabel4.setText("조편성조회 기간");

        jDateFromGroupDate.setDateFormatString("yyyy-MM-dd");

        jTableGroupListTable.setModel(new Golf_Manager.model.GroupDescModel());
        jTableGroupListTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTableGroupListTable.getTableHeader().setReorderingAllowed(false);
        jTableGroupListTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableGroupListTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableGroupListTable);

        jDateFromGroupDate.setDateFormatString("yyyy-MM-dd");

        jLabel1.setText("~");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jDateFromGroupDate, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jDateToGroupDate, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jDateFromGroupDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(okButton)
                    .addComponent(jDateToGroupDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 338, Short.MAX_VALUE)
                .addContainerGap())
        );

        getRootPane().setDefaultButton(okButton);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
         if(jDateFromGroupDate.getDate() != null && jDateToGroupDate.getDate() != null){
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
             if(searchGroupList(sdf.format(jDateFromGroupDate.getDate()), sdf.format(jDateToGroupDate.getDate())) < 0){
                JOptionPane.showMessageDialog(rootPane, "검색결과가 없습니다.", "확인", WIDTH);
            } else {
                JOptionPane.showMessageDialog(rootPane, "조회 되었습니다.", "확인", WIDTH);
             }
         } else {
             JOptionPane.showMessageDialog(rootPane, "조편성조회 기간을 입력하십시오.", "확인", WIDTH);
         }    
    }//GEN-LAST:event_okButtonActionPerformed

    private void jTableGroupListTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableGroupListTableMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jTableGroupListTableMouseClicked
    
    private int searchGroupList(String groupDateFrom, String groupDateTo){
        List<GroupDesc> groupDescs = GroupDAO.selectGroupMstBetween(groupDateFrom, groupDateTo);
        GroupDescModel groupDescModel = (GroupDescModel)jTableGroupListTable.getModel();
        groupDescModel.setGroupDescs(groupDescs);
        return groupDescs.size();
    }
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.toedter.calendar.JDateChooser jDateFromGroupDate;
    private com.toedter.calendar.JDateChooser jDateToGroupDate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableGroupListTable;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables

    private String returnGdate = "";
}
